﻿using System;
using System.ServiceModel;
using System.ServiceProcess;
using AccessControl.Utility.Tools;

namespace AccessControl.AccessControlAgent
{
    [ServiceBehavior(
ConcurrencyMode = ConcurrencyMode.Single,
InstanceContextMode = InstanceContextMode.Single)]
    public class ACServiceAgent : IACServiceAgent{
        private DateTime lastUserAction;
        public ServiceControllerStatus GetServiceStatus(){
            return ServiceManager.GetServiceStatus();
        }

        public bool StartService()
        {
            if (lastUserAction.AddSeconds(1) > DateTime.Now)
                return false;
            lastUserAction = DateTime.Now;

            AccessControlAgent.ServiceInstance.WriteLog("Service started by user", eventLevelConstants.Info);
            
            var retval= ServiceManager.StartServie();
            AccessControlAgent.ServiceInstance.StartMonitoring();
            return retval;
        }

        public bool StopService()
        {
            if (lastUserAction.AddSeconds(5) > DateTime.Now)
                return false;
            lastUserAction = DateTime.Now;
            
            AccessControlAgent.ServiceInstance.WriteLog("Service stopped by user",eventLevelConstants.Info);
            AccessControlAgent.ServiceInstance.StopMonitoring();
            return ServiceManager.StopService();
        }

        public bool RestartService(){
            if (lastUserAction.AddSeconds(5) > DateTime.Now)
                return false;
            lastUserAction = DateTime.Now;
            AccessControlAgent.ServiceInstance.WriteLog("Service restarted by user", eventLevelConstants.Info);
            
            var retval= ServiceManager.RestartService();

            AccessControlAgent.ServiceInstance.StartMonitoring();

            return retval;
        }

    }
}
